package com.buildings.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.adapter.ProjectListAdapter;
import com.buildings.adapter.TaskListAdapter;
import com.buildings.adapter.TaskListNewAdapter;
import com.buildings.adapter.TaskPagerAdapter;
import com.buildings.api.Api;
import com.buildings.base.BaseActivity;
import com.buildings.bean.ProjectBean;
import com.buildings.bean.ProjectListBean;
import com.buildings.bean.TaskBean;
import com.buildings.bean.TaskListNewBean;
import com.buildings.bean.TaskNewBean;
import com.buildings.fragment.TaskListFragment;
import com.buildings.http.BaseCallBack;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.DialogUtils;
import com.buildings.util.SPUtil;
import com.buildings.view.vpindicator.UnderlinePageIndicator;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 任务列表
 */
public class TaskListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_right_text)
    TextView tvightText;
    private int pageNum = 1;
    private int total = 10;
    private TaskListNewAdapter mAdapter;
    private List<TaskNewBean> datas = new ArrayList<>();
    private String projectId;
    private String projectName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_list);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        http();
    }

    private void init() {
        tvTitle.setText("任务列表");
        tvightText.setText("新建任务");
        if (!getIntent().hasExtra("projectId")) {
            finish();
        }
        if (!getIntent().hasExtra("projectName")) {
            finish();
        }
        projectId = getIntent().getStringExtra("projectId");
        projectName = getIntent().getStringExtra("projectName");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mAdapter = new TaskListNewAdapter(this, datas);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new TaskListNewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //查看表单
                Intent intent = new Intent(activity, SeeTableListActivity.class);
                intent.putExtra("projectId", projectId);
                intent.putExtra("projectName", projectName);
                intent.putExtra("taskName", datas.get(position).getTaskName());
                intent.putExtra("task", datas.get(position).getTaskId());
                startActivity(intent);
            }

            @Override
            public void onTask(View view, int position) {
                //修改任务
                Intent intent = new Intent(activity, TaskNewActivity.class);
                intent.putExtra("projectId", projectId);
                intent.putExtra("projectname", projectName);
                intent.putExtra("taskName", datas.get(position).getTaskName());
                intent.putExtra("task", datas.get(position));
                intent.putExtra("type", 2);
                startActivity(intent);
            }

            @Override
            public void onForm(View view, int position) {
                //执行任务
                Intent intent = new Intent(activity, MapTableActivity.class);
                intent.putExtra("projectId", projectId);
                intent.putExtra("projectName", projectName);
                intent.putExtra("taskName", datas.get(position).getTaskName());
                intent.putExtra("taskId", datas.get(position).getTaskId());
                intent.putExtra("TaskNewBean", datas.get(position));
                startActivity(intent);
            }
        });

        refreshLayout .setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (datas.size() >= total) {
                    CustomToast.showToast(TaskListActivity.this, "没有更多的任务");
                    refreshlayout.finishLoadmore();
                } else {
                    //todo 网络请求
                    pageNum++;
                    http();
                }
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                //todo 网络请求
                http();
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_right_text})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right_text:
                Intent intent = new Intent(activity, TaskNewActivity.class);
                intent.putExtra("projectId", projectId);
                intent.putExtra("projectname", projectName);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
        }
    }

    private void http() {
        String url = Api.QUERY_HDD_TASK + "?curPage=" + pageNum + "&projectId=" + projectId + "&pageSize=10";
        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
//                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<TaskListNewBean>() {
                    @Override
                    public void onSuccess(TaskListNewBean bean) {
                        super.onSuccess(bean);
                        refreshLayout.finishLoadmore();
                        refreshLayout.finishRefresh();
                        if (bean != null && bean.getCode() == 200) {
                            if (pageNum == 1) {
                                datas.clear();
                            }
                            total = bean.getData().getTotal();
                            List<TaskNewBean> result = bean.getData().getRows();
                            datas.addAll(result);
                            mAdapter.notifyDataSetChanged();
                        } else {
                            CustomToast.showToast(TaskListActivity.this, "获取失败");
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        TaskListActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                CustomToast.showToast(TaskListActivity.this, "网络异常");
                            }
                        });
                        refreshLayout.finishLoadmore();
                        refreshLayout.finishRefresh();
                    }
                });

    }
}
